<template>
<div class="message" ref="message" v-if="session">
    <ul v-if="session">
        <li v-for="(item,index) in session.messages">
            <p v-if="index % 5 == 0" class="time">
                <span>{{ item.date }}</span>
            </p>
            <div class="main" :class="{ self : item.self}">
                <img class="avatar" width="40" height="40" :src="item.self ? 'images/1.jpg' : 'images/3.jpg'" />
                <div class="name">{{ item.name }}</div>
                <div class="text">
                    {{ item.content }}
                </div>
            </div>
        </li>
    </ul>
</div>
</template>

<script>
    export default {
        props : ['session'],
        //每次页面渲染完之后滚动条在最底部
        updated:function(){
            this.scrollToBottom();
        },
        methods : {
            scrollToBottom: function () {
                this.$nextTick(() => {
                    this.$refs.message.scrollTop = this.$refs.message.scrollHeight;
                })
            }
        }
    };
</script>

<style lang="less" scoped>
.message {
    padding: 10px 15px;
    overflow-y: scroll;

    li {
        margin-bottom: 15px;
    }
    .time {
        margin: 0px 0px 3px 0px;
        text-align: center;
        > span {
            display: inline-block;
            padding: 0 8px;
            font-size: 12px;
            color: #fff;
            border-radius: 8px;
            background-color: #dcdcdc;
        }
    }
    .name{
        font-size: 10px;
        color: #B2B2B2;
    }
    .avatar {
        float: left;
        margin: 0 10px 0 0;
        border-radius: 3px;
    }
    .text {
        display: inline-block;
        position: relative;
        padding: 0 10px;
        max-width: ~'calc(100% - 40px)';
        min-height: 30px;
        line-height: 2.5;
        font-size: 12px;
        text-align: left;
        word-break: break-all;
        background-color: #fafafa;
        border-radius: 4px;

        &:before {
            content: " ";
            position: absolute;
            top: 9px;
            right: 100%;
            border: 6px solid transparent;
            border-right-color: #fafafa;
        }
    }

    .self {
        text-align: right;

        .avatar {
            float: right;
            margin: 0 0 0 10px;
        }
        .text {
            background-color: #b2e281;

            &:before {
                right: inherit;
                left: 100%;
                border-right-color: transparent;
                border-left-color: #b2e281;
            }
        }
    }
}
</style>